// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   TripleDES.java

package ar.com.profertil.utils.service;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

// TODO: Auto-generated Javadoc
/**
 * The Class TripleDES.
 */
public class TripleDES {

	/**
	 * Instantiates a new triple des.
	 */
	public TripleDES() {
	}

	/**
	 * Encriptar.
	 * 
	 * @param inputFile
	 *            the input file
	 * @param outputFile
	 *            the output file
	 * @param key
	 *            the key
	 * @param encriptar
	 *            the encriptar
	 */
	public static void encriptar(String inputFile, String outputFile, Key key, boolean encriptar) {
		try {
			java.security.Provider p = new BouncyCastleProvider();
			Security.addProvider(p);
			Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
			if (encriptar) {
				cipher.init(1, key);
			} else {
				cipher.init(2, key);
			}
			FileInputStream in = new FileInputStream(inputFile);
			FileOutputStream fileOut = new FileOutputStream(outputFile);
			CipherOutputStream out = new CipherOutputStream(fileOut, cipher);
			byte buffer[] = new byte[1024];
			int length;
			while ((length = in.read(buffer)) != -1) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Encriptar.
	 * 
	 * @param input
	 *            the input
	 * @param outputFile
	 *            the output file
	 * @param key
	 *            the key
	 * @param encriptar
	 *            the encriptar
	 * @return the encripted string
	 * @throws IOException 
	 * @throws InvalidKeyException 
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 */
	public static String encriptar(String input, Key key, boolean encriptar) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
		java.security.Provider p = new BouncyCastleProvider();
		Security.addProvider(p);
		Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
		if (encriptar) {
			cipher.init(1, key);
		} else {
			cipher.init(2, key);
		}
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();
		CipherOutputStream out = new CipherOutputStream(bOut, cipher);
		out.write(input.getBytes());
		out.close();
		return bOut.toString();
	}

	/** The Constant kBufferSize. */
	public static final int kBufferSize = 1024;
}
